/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.util;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractAction;
import javax.swing.Action;

/**
 * ActionWrapper : The action wrapper class.
 * 
 * @author Romain DUBOIS
 */
public class ActionWrapper extends AbstractAction implements PropertyChangeListener {

    /** Serial version */
    private static final long serialVersionUID = 1L;

    /** The wrapped action */
    private final Action mWrapped;
    /** The local enabled state */
    private boolean mEnabled;

    /**
     * Constructor.
     * 
     * @param pAction
     *            The action to wrap
     */
    public ActionWrapper(final Action pAction) {
        super();
        mWrapped = pAction;
        mWrapped.addPropertyChangeListener(this);
        mEnabled = true;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(final ActionEvent pE) {
        mWrapped.actionPerformed(pE);
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.AbstractAction#getValue(java.lang.String)
     */
    @Override
    public Object getValue(final String pKey) {
        Object lValue = super.getValue(pKey);
        if (lValue == null) {
            lValue = mWrapped.getValue(pKey);
        }
        return lValue;
    }

    /**
     * {@inheritDoc}
     * 
     * @see javax.swing.AbstractAction#setEnabled(boolean)
     */
    @Override
    public void setEnabled(final boolean pNewValue) {
        mEnabled = pNewValue;
        super.setEnabled(mWrapped.isEnabled() && mEnabled);
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    @Override
    public void propertyChange(final PropertyChangeEvent pEvt) {
        final String lProp = pEvt.getPropertyName();

        // Notify only for:
        // - Property which are not overriden
        // - Enabled if the result state changes (= if wrapped is enabled)
        if (super.getValue(lProp) == null || ("enabled".equals(lProp) && mEnabled)) { //$NON-NLS-1$
            firePropertyChange(pEvt.getPropertyName(), pEvt.getOldValue(), pEvt.getNewValue());
        }
    }
}
